//Hard to Kill - Drowning (Code to survive upon air timer expiring underwater or in space at the expense of 20% energy per second)
/*addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target.player and target.player.valid then
		if target.player.mo.skin ~= "annie_belnades" then --This should only work when playing as Annie
			return
		end
		
		if damagetype == DMG_DROWNED
		and (target.player.mo.drowndelay ~= 1 or target.player.mo.energy >= 20*FRACUNIT) then
			return false --As long as the drown delay variable is above 1 (0 is inactive) and sufficient energy remains, Annie can't drown
		end
	end
end, MT_PLAYER)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target.player and target.player.valid then
		if target.player.mo.skin ~= "annie_belnades" then --This should only work when playing as Annie
			return
		end
		
		if damagetype == DMG_SPACEDROWN
		and (target.player.mo.suffocatedelay ~= 1 or target.player.mo.energy >= 20*FRACUNIT) then
			return false --As long as the suffocate delay variable is above 1 (0 is inactive) and sufficient energy remains, Annie can't suffocate 
		end
	end
end, MT_PLAYER)

addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid then
	
		if player.mo.skin ~= "annie_belnades" then
			return
		end
		
		if player.mo.drowndelay == nil then --Setting up the "drown delay" variable that governs survival without air
			player.mo.drowndelay = 0
		end
		
		if player.mo.suffocatedelay == nil then --Setting up the "suffocate delay" variable that governs survival without air
			player.mo.suffocatedelay = 0
		end
		
		if player.powers[pw_underwater] ~= 1 then --Drown delay doesn't start counting down until the standard air timer runs out
			player.mo.drowndelay = 0
		end
		
		if player.powers[pw_spacetime] ~= 1 then -- Suffocate delay doesn't start counting down until the standard air timer runs out
			player.mo.suffocatedelay = 0
		end
		
		if player.powers[pw_underwater] == 1
		and player.mo.drowndelay == 0
		and player.mo.energy >= 20*FRACUNIT then
			player.mo.drowndelay = 1*TICRATE --Delay drowning for 1 second (prevent death); start delay timer
			if not (leveltime%TICRATE) then --making sure energy is only drained once per second
				player.mo.energy = $ - 20*FRACUNIT --Reduce player.mo.energy by 20*FRACUNIT
			end
			player.mo.deathdefiance = 1 --Stop energy regeneration until player gets air or dies
		end
		
		if player.powers[pw_spacetime] == 1
		and player.mo.suffocatedelay == 0
		and player.mo.energy >= 20*FRACUNIT then
			player.mo.suffocatedelay = 1*TICRATE --Delay suffocation for 1 second (prevent death); start delay timer
			if not (leveltime%TICRATE) then --making sure energy is only drained once per second
				player.mo.energy = $ - 20*FRACUNIT --Reduce player.mo.energy by 20*FRACUNIT
			end
			player.mo.deathdefiance = 1 --Stop energy regeneration until player gets air or dies
		end
		
		if player.mo.drowndelay > 0 then --If player.mo.drowndelay > 0, that means player.powers[pw_underwater] == 1, which is when most characters would have drowned.
			--code to prevent usual player death-by-drowning here then
			if (leveltime%TICRATE) then
				player.mo.drowndelay = $ - 1
				if player.mo.eflags & MFE_UNDERWATER then
					player.powers[pw_underwater] = 1
				end
			end
		end
		
		if player.mo.suffocatedelay > 0 then --If player.mo.suffocatedelay > 0, that means player.powers[pw_spacetime] == 1, which is when most characters would have suffocated.
			--code to prevent usual player death-by-suffocation here then
			if (leveltime%TICRATE) then
				player.mo.suffocatedelay = $ - 1
				if P_InSpaceSector(player.mo) == true then
					player.powers[pw_spacetime] = 1
				end
			end
		end

		if player.mo.drowndelay == 1 then --Check if player is still drowning after 1-second delay period ends
			if player.powers[pw_underwater] ~= 1 then --if player has gotten air, end the loop
				player.mo.drowndelay = 0
				return
			else
				if player.mo.energy >= 20*FRACUNIT --if player has enough energy to continue surviving without air, continue the loop
				and player.powers[pw_underwater] == 1 then --making ABSOLUTELY SURE this code only runs when out of air
					player.mo.drowndelay = 1*TICRATE --reset drown delay timer to 1 second
					if not (leveltime%TICRATE) then --making sure energy is only drained once per second
						player.mo.energy = $ - 20*FRACUNIT --reduce energy by 20
					end
					player.mo.deathdefiance = 1 --energy regeneration remains paused*/
/*				else
					P_DamageMobj(player.mo, nil, nil, 1, DMG_DROWNED)*/ --If energy is insufficient to stay alive, Annie drowns and dies
/*						target = MT_PLAYER
						inflictor = nil
						source = nil
						damage = 1
						damagetype = DMG_DROWNED */ --code removed because it caused instant drowning upon game start for some reason
/*				end
			end
		end
			
		if player.mo.suffocatedelay == 1 then --Check if player is still suffocating after 1-second delay period ends
			if player.powers[pw_spacetime] ~= 1 then --if player has gotten air, end the loop
				player.mo.suffocatedelay = 0
				return
			else
				if player.mo.energy >= 20*FRACUNIT --if player has enough energy to continue surviving without air, continue the loop
				and player.powers[pw_spacetime] == 1 then --making ABSOLUTELY SURE this code only runs when out of air
					player.mo.suffocatedelay = 1*TICRATE --reset suffocation delay timer to 1 second
					if not (leveltime%TICRATE) then --making sure energy is only drained once per second
						player.mo.energy = $ - 20*FRACUNIT --reduce energy by 20
					end
					player.mo.deathdefiance = 1 --energy regeneration remains paused*/
/*				else
					P_DamageMobj(player.mo, nil, nil, 1, DMG_SPACEDROWN)*/ --If energy is insufficient to stay alive, Annie suffocates and dies
/*						target = MT_PLAYER
						inflictor = nil
						source = nil
						damage = 1
						damagetype = DMG_SPACEDROWN */ --code removed because it caused instant suffocation upon game start for some reason
/*				end
			end
		end
	end
end)

addHook("TouchSpecial", function(special, toucher)
	if special and special.valid then
		if toucher.player and toucher.player.valid then
			if toucher.player.mo.skin ~= "annie_belnades" then
				return
			end
		end
		
		toucher.player.mo.drowndelay = 0 --return drowndelay to 0 upon touching an air bubble
		toucher.player.powers[pw_underwater] = underwatertics --making sure that pw_underwater resets as intended
		end
	end, MT_EXTRALARGEBUBBLE)*/